package com.ccjq.shosestore.shoppingcart.service.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.core.utils.PojoUtils;
import com.ccjq.shosestore.order.bo.OrderBo;
import com.ccjq.shosestore.order.bo.ShopInfoBo;
import com.ccjq.shosestore.order.service.OrderService;
import com.ccjq.shosestore.order.service.impl.OrderServiceImpl;
import com.ccjq.shosestore.shoppingcart.bo.SaveCartBo;
import com.ccjq.shosestore.shoppingcart.bo.ShowCartBo;
import com.ccjq.shosestore.shoppingcart.dao.ShoppingCartDao;
import com.ccjq.shosestore.shoppingcart.dao.impl.ShoppingCartDaoImpl;
import com.ccjq.shosestore.shoppingcart.model.SaveCartModel;
import com.ccjq.shosestore.shoppingcart.model.ShowCartModel;
import com.ccjq.shosestore.shoppingcart.service.ShoppingCartService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车的CRUD接口的实现类
 * 
 * @author 李佳鑫
 * */
public class ShoppingCartServiceImpl implements ShoppingCartService {
	
	public List<ShowCartBo> saveCart(SaveCartBo saveCartBo) {
		List<ShowCartBo> showCarts = new ArrayList<ShowCartBo>();
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		int rowNum = shoppingCartDao.addCart(PojoUtils.bo2po(saveCartBo, SaveCartModel.class));
		if(rowNum > 0){
			//如果添加购物车成功 就调用显示该购物车页面
			showCarts = queryCartById(saveCartBo.getUserId());
		}
		return showCarts;
	}
	
	public List<ShowCartBo> queryCartById(String userId){
		List<ShowCartBo> showCarts = new ArrayList<ShowCartBo>();
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		List<ShowCartModel> cartsList = shoppingCartDao.queryCartById(userId);
		//bo和model模型转换
		for(ShowCartModel ShowCartModel : cartsList){
			showCarts.add(PojoUtils.po2bo(ShowCartModel, ShowCartBo.class));
		}
		return showCarts;
	}
	
	public boolean delCartById(String shoppingCartId){
		boolean sign = false;
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		int row = shoppingCartDao.delCartById(shoppingCartId);
		if(row > 0){
			sign = true;
		}
		return sign;
	}

	public boolean updateCartById(int quantity, String shoppingCartId) {
		boolean sign = false;
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		int row = shoppingCartDao.updateCartById(quantity, shoppingCartId);
		if(row > 0){
			sign = true;
		}
		return sign;
	}


	public List<OrderBo> commitOrder(List<ShowCartBo> cartS,OrderBo orderBo) {
		//获得connection对象
		Connection con = DbConnection.getConnection();
		//作为返回值
		List<OrderBo> orders = new ArrayList<OrderBo>();
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		List<ShopInfoBo> infos = new ArrayList<ShopInfoBo>();
		ShopInfoBo shopInfoBo = null;
		int i = 0;
		//当购物车中有商品时
		if(cartS.size() > 0){
			//遍历购物车中的商品
			for(ShowCartBo cart : cartS){
				//查询购物车中商品的库存
				int stock = shoppingCartDao.queryStockById(cart.getProductId());
				//如果库存大于要购买的数量  可以进行提交订单
				if(stock >= cart.getQuantity()){
					i++;
				}else{
					return orders;
					//break;
				}
			}
			if(i == cartS.size()){
				System.out.println("库存大于要购买的数量");
				for(ShowCartBo cart : cartS){
					//杨哥定义的ShopInfoBo数据类型
					shopInfoBo = new ShopInfoBo();
					shopInfoBo.setProduceId(cart.getProductId());
					shopInfoBo.setDescripyion(cart.getDescription());
					shopInfoBo.setPrice(cart.getPrice());
					shopInfoBo.setQuantity(cart.getQuantity());
					shopInfoBo.setSubtotal(cart.getTotal());
					infos.add(shopInfoBo);
				}
				//将商品的list集合放到orderBo模型中
				orderBo.setShopInfo(infos);
				OrderService orderService = new OrderServiceImpl();
				try {
					//将自动改为手动
					con.setAutoCommit(false);
					orders = orderService.saveOrder(orderBo);
					//修改库存数量
					for(ShowCartBo cart : cartS){
						shoppingCartDao.updateStockById(cart.getQuantity(), cart.getProductId());
					}
					//清空购物车
					shoppingCartDao.emptyCart(orderBo.getUserId());
					con.commit();
				} catch (SQLException e) {
					e.printStackTrace();
						try {
							con.rollback();
							con.setAutoCommit(true);
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
				}
			}
		}
		return orders;
	}

	public double queryTotal(String userId) {
		ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
		double total = shoppingCartDao.queryTotal(userId);
		return total;
	}
}
